package com.sanpu.kxe.module.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.repay.interfaces.IRefundCountListener;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.utils.ToastUtil;

import butterknife.ButterKnife;

/**
 * Description：还款天数选择
 * Created by 薛勇军 on 2019/5/28.
 */

public class RefundCountChooseDialog {

    private TextView subTv, repayCountTv, addTv;

    private RoundTextView createPlanRtv;

    private IRefundCountListener countListener;

    private Dialog aDialog;

    private String refundCount, maxRefundCount, minRefundCount;

    public void setListener(IRefundCountListener countListener) {
        this.countListener = countListener;
    }

    public RefundCountChooseDialog(Context context, String refundCount, String maxRefundCount, String minRefundCount) {
        this.refundCount = refundCount;
        this.maxRefundCount = maxRefundCount;
        this.minRefundCount = minRefundCount;
        GoodsChooseDialog(context);
    }

    public void GoodsChooseDialog(Context context) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_refund_count_choose, null);
        aDialog = new Dialog(context, R.style.bottom_pop_menu);
        subTv = ButterKnife.findById(inflate, R.id.subTv);
        repayCountTv = ButterKnife.findById(inflate, R.id.repayCountTv);
        addTv = ButterKnife.findById(inflate, R.id.addTv);
        createPlanRtv = ButterKnife.findById(inflate, R.id.createPlanRtv);
        View emptyView = ButterKnife.findById(inflate, R.id.emptyView);
        emptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aDialog.dismiss();
            }
        });

        setUiBeforShow(context);

        aDialog.setContentView(inflate); // 把自定義view加上去
        aDialog.setCanceledOnTouchOutside(false);
        aDialog.setCancelable(false);
        aDialog.show();
    }

    public void setUiBeforShow(Context context) {
        if (!TextUtils.isEmpty(refundCount)) {
            repayCountTv.setText(refundCount);
        }
        subTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int currentCount = Integer.parseInt(repayCountTv.getText().toString());
                int minCount = TextUtils.isEmpty(minRefundCount) ? 0 : Integer.parseInt(minRefundCount);
                if (currentCount > minCount) {
                    repayCountTv.setText(String.valueOf(currentCount - 1));
                } else {
                    ToastUtil.showTextViewPrompt("已达到最小还款天数！");
                }
            }
        });
        addTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int currentCount = Integer.parseInt(repayCountTv.getText().toString());
                int maxCount = TextUtils.isEmpty(maxRefundCount) ? 99 : Integer.parseInt(maxRefundCount);
                if (currentCount < maxCount) {
                    repayCountTv.setText(String.valueOf(currentCount + 1));
                } else {
                    ToastUtil.showTextViewPrompt("已达到最大还款天数！");
                }
            }
        });
        createPlanRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String refundStr = repayCountTv.getText().toString();
                if (countListener != null && !TextUtils.equals(refundStr, refundCount)) {
                    countListener.onChangeRefundCount(refundStr);
                }
                dismiss();
            }
        });
    }

    public void dismiss() {
        if (aDialog != null && aDialog.isShowing()) {
            aDialog.dismiss();
        }
    }
}
